/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xdeserializer.h
 *
 * Purpose: deserializer API define and implementation
 *
 * Developer:
 *   wen.gu , 2023-08-08
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_PROTOCOL_XDESERIALIZER_H__
#define __XBUS_PROTOCOL_XDESERIALIZER_H__

#include <cstdint>
#include <vector>
#include <array>
#include <string>
#include <map>

#include "xbus/protocol/xdesializable.h"


namespace xbus::protocol {

class XDeserializer {
public:
    XDeserializer();
    virtual ~XDeserializer();
public:
    virtual bool deserialize(bool& val);
    virtual bool deserialize(int8_t& val);
    virtual bool deserialize(int16_t& val);
    virtual bool deserialize(int32_t& val);
    virtual bool deserialize(int64_t& val);    
    virtual bool deserialize(uint8_t& val);
    virtual bool deserialize(uint16_t& val);
    virtual bool deserialize(uint32_t& val);
    virtual bool deserialize(uint64_t& val); 
    virtual bool deserialize(float& val);
    virtual bool deserialize(double& val);
    virtual bool deserialize(std::string& val); /** deserialize string */
    virtual bool deserialize(XDeserializable& deser);

public: 
    /** deserialize for enum type*/
    template<typename T, typename std::enable_if<std::is_enum<T>::value >::type >
    bool deserialize(T& val) { return deserialize(static_cast<uint32_t&>(val)); }

    /** deserialize for vecotr. treat Optional as dynamic array, |val.size()| element values...| */
    template<typename T, typename std::enable_if<!(std::is_integral<T>::value ||  std::is_floating_point<T>::value) >::type>
    bool deserialize(std::vector<T>& val) {
        uint32_t size;
        if (!deserialize(size)) { return false; }
        val.reserve(size);

        for (size_t i; i < size; i++) {
            T tmp;
            if (deserialize(tmp)) {
                val.push_back(std::move(tmp));
            } else {
                val.clear();
                return false;
            }
        }

        return true;
    }

    template<typename T, typename std::enable_if<(std::is_integral<T>::value ||  std::is_floating_point<T>::value) >::type>
    bool deserialize(std::vector<T>& val) {
        uint32_t size;
        if (!deserialize(size)) { return false; }

        if (size > 0) {
            val.resieze(size);
            return deserialize_l((uint8_t*)val.data(), size * sizeof(T));
        }

        return true;
    }

    /** deserialize for array */
 template<typename T, std::size_t N, typename std::enable_if<!(std::is_integral<T>::value ||  std::is_floating_point<T>::value) >::type>
    bool deserialize(std::array<T, N>& val) {
        uint32_t size;
        if (!deserialize(size)) { return false; }

        if (size <= N) {
            for (size_t i; i < size; i++) {
                if (!deserialize(val[i])) {
                    val.clear();
                    return false;
                }
            }
        }

        return true;
    }

    template<typename T, std::size_t N, typename std::enable_if<(std::is_integral<T>::value ||  std::is_floating_point<T>::value) >::type>
    bool deserialize(std::array<T, N>& val) {
        uint32_t size;
        if (!deserialize(size)) { return false; }

        if (size <= N) { return deserialize_l((uint8_t*)val.data(), size * sizeof(T)); }

        return true;
    }

    /** deserialize for map */
    template<typename Key, typename Value>
    bool deserialize(std::map<Key, Value>& val) {
        uint32_t size;
        if (deserialize(size)) {
            for (size_t i; i < size; i++) {
                Key tmp_key;
                Value tmp_val;
                if (deserialize(tmp_key) && deserialize(tmp_val)) {                                
                    val[tmp_key] = std::move(tmp_val);
                } else {
                    val.clear();
                    return false;
                }
            }
            return true;
        }

        return false;      
    }

    template<typename T, typename ...Args>
    bool deserialize(T& t, Args&... args) {
        if (!deserialize(std::forward<T&>(t))) { return false; }
        return deserialize(std::forward<Args&>(args)...);
    }

public:
    /** set a new payload to deserialize, will reset the state of deserialize */
    virtual bool reset(uint8_t* buf, uint32_t length);

protected:
    virtual bool deserialize_l(uint8_t* data, uint32_t size);
    virtual bool deserialize();  /** nothing todo, just for end the bool deserialize(T& t, Args&... args) */
};


} //namespace xbus::protocol


#endif /** !__XBUS_PROTOCOL_XDESERIALIZER_H__ */